package com.shushan.pages;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.android.net.bean.NetRetBean;
import com.android.net.core.common.RequestType;
import com.android.net.helper.NetHelper;
import com.android.net.listener.common.CallbackCode;
import com.android.net.url.UrlParse;
import com.shushan.base.NavigationLoseActivity;
import com.shushan.exam.ExamActivity;
import com.shushan.home.bean.RewardTimeBean;
import com.shushan.network.UrlCommParse;
import com.shushan.network.UrlConst;
import com.shushan.network.listener.NetStrCommListener;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.shushanbao.R;
import com.shushan.util.Session;

/**
 * @ClassName: GetSmartTimeActivity
 * @Desciption: 智能练习我要玩手机
 * @author: yichaohua
 * @date: 2018-07-21
 */
public class GetSmartTimeActivity extends NavigationLoseActivity implements View.OnClickListener {

    private static final String TAG = "GetSmartTimeActivity";

    private TextView tvTask;
    private Button btnGoComplete;
    private ListView lvTime;

    private BabyApplication shuShanApplication = BabyApplication.getInstance();
    private int type = 2;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_time_award_details);

        setTitle("练习获得手机使用时间");
        
        initView();

        getData();
    }

    private void initView() {
        btnGoComplete = findViewById(R.id.btn_details_go_complete);
        tvTask = findViewById(R.id.tv_complete_condition);
        lvTime = findViewById(R.id.list_view);

        btnGoComplete.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_details_go_complete:
                onBackAction();

                RewardTimeBean rewardTimeBean = new RewardTimeBean();
                rewardTimeBean.practice_type = 1;
                Session.getSession().put("rewardTimeBean",rewardTimeBean);

                Intent intent = new Intent(this,ExamActivity.class);
                intent.putExtra("awardType",1);
                startActivityAllow(intent);
                break;
            default:
                break;
        }
    }

    private class MyListAdapter extends BaseAdapter {
        private String[] timeArray;

        public MyListAdapter(String[] timeArray) {
            this.timeArray = timeArray;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if(convertView == null){
                holder = new ViewHolder();
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_score_time,null,false);
                //holder.tvScore = convertView.findViewById(R.id.tv_score);
                holder.tvTime = convertView.findViewById(R.id.tv_time);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }

            holder.tvTime.setText(getTimeDes(position,timeArray[position]));

            return convertView;
        }

        @Override
        public Object getItem(int position) {
            return timeArray[position];
        }

        @Override
        public int getCount() {
            return timeArray.length;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        class ViewHolder{
            //private TextView tvScore;
            private TextView tvTime;
        }
    }

    private void getData() {
        int childId = shuShanApplication.getCurrentChildId();
        showWaitDialog();

        UrlParse urlParse = new UrlCommParse(this, UrlConst.BASE_URL).appendRegion(UrlConst.GET_ZN_TIME);
        UrlParse paramParse = new UrlCommParse(this)
                .putValue("child_id", childId);
        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(paramParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetStrCommListener() {
                    @Override
                    protected void onCommon() {
                        dismissWaitDialog();
                    }

                    @Override
                    protected void onSuccess(String data) {
                        if(!TextUtils.isEmpty(data)){
                            String[] timeArray = data.split(",");
                            lvTime.setAdapter(new MyListAdapter(timeArray));
                        }else {
                            showToast("获取数据为空");
                            return;
                        }

                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                        showNetError(netRetBean);
                    }
                });
    }

    public String getTimeDes(int position,String time){
        StringBuffer sb = new StringBuffer();
        switch (position){
            case 0:
                sb.append("本次练习获得70分以下");
                break;
            case 1:
                sb.append("本次练习获得70-79分");
                break;
            case 2:
                sb.append("本次练习获得80-89分");
                break;
            case 3:
                sb.append("本次练习获得90-99分");
                break;
            case 4:
                sb.append("本次练习获得100分");
                break;
        }
        sb.append(",使用").append(time).append("分钟手机");
        return sb.toString();
    }
}
